--[[
	获取钻石战绩
]]
local filename = "getgameresult.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog =require "filelog"
local configdao = require "configdao"

local service = nil
local CMD = {
	service = nil
}

-- 同时创建目录
local function get_file_name(filename)
	----- 日志分日期存储 ，所以要先检查目录------
	if not filename then return '' end
	local _, now = string.match(filename,"(%a+_%d+_%d+_%d+)_(%d+)")
   	local log_path = os.date("%Y_%m_%d", now) .."/" .. os.date("%H", now)

	return log_path .. "/"..filename .. ".log"
end

-- socket传来的请求，可以获取自己的信息，也可以去获取其他agent中的个人信息
-- 如果都不在，则去数据节点中获取
function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end
	local tablename = "rm_diamondresult"
	local gameresult = {}
	if msgbody.resulttype == 2 then
		tablename = "rm_fangkaresult"
	elseif msgbody.resulttype == 3 then
		tablename = "rm_coinresult"
	end
	local result = cluster.call("datanode", ".router", "select_rk", rid, tablename)
	local i = 1
	for k,v in pairs(result[2]) do
        local table_code,round_code = tonumber(v.table_code),nil
        if table_code > 1000000 then
            round_code = string.format("%04d",table_code % 10000)
            table_code = math.floor(table_code / 10000)
        end

		gameresult[i] = {}
		gameresult[i].keyid				= v.keyid
		gameresult[i].game_type			= v.game_type
		gameresult[i].table_code		= table_code
		gameresult[i].self_result		= v.self_result
        gameresult[i].self_otherresult  = v.self_otherresult
		gameresult[i].other_result_json	= v.other_result_json
		gameresult[i].create_time		= v.create_time
		gameresult[i].round_code		= round_code
		gameresult[i].replay_logpath	= configdao.get_cfgsvrs_key(skynet.getenv("nodename")).relpay_logpath .. get_file_name(v.replay_logname)
		i = i+1
	end
	return {gameresult = gameresult, resulttype = msgbody.resulttype}
end

return CMD